package zirao.base.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import zirao.base.R;
import zirao.base.immersion.ImmersionBar;
import zirao.base.manager.BaseActivityManager;

/**
 * Created by ${lixiaonan} on 2018/7/26.
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{

    protected ImmersionBar mImmersionBar;
    private int contentViewId, titleViewID;

    public BaseActivity(int contentViewId, int titleViewID) {
        this.contentViewId = contentViewId;
        this.titleViewID = titleViewID;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);												//取消标题栏（ActionBar）
        this.setContentView(this.contentViewId);
        //初始化沉浸状态栏
        if (enableImmersionBar()) {
            initImmersionBar();
        }
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);						//禁止横屏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);	//启动activity时不自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);			//软键盘不影响布局

        BaseActivityManager.getAppManager().addActivity(this);                                      //添加Activity到Activity管理器堆栈
        overridePendingTransition(R.anim.activity_slide_in_right, R.anim.activity_hold);            //滑动式切换界面

        this.initView();
        this.initListener();
        this.initData();
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean enableImmersionBar() {
        if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) && (this.titleViewID != 0)){
            return true;
        }else{
            return false;
        }
    }

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
        if(this.titleViewID != 1){
            mImmersionBar
                    .titleBar(this.titleViewID)
                    .keyboardEnable(true)
                    .init();
        }
    }

    public abstract void onBackPressed();

    protected abstract void initView();
    protected abstract void initData();

    protected abstract void initListener();
    protected abstract void widgetClick(View v);

    @Override
    public void onClick(View v) {
        this.widgetClick(v);
    }

    public void showToast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    public void outLog(String Tag, String msg){
        Log.i(Tag,msg);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_hold, R.anim.activity_slide_out_right);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 页面跳转
     * @param  clz
     * */
    public void openActivity(Class<?> clz) {
        openActivity(clz, null);
    }

    /**
     * 带数据的页面跳转
     * @param  clz
     * @param  bundle
     * */
    public void openActivity(Class<?> clz, Bundle bundle) {
        openActivity(clz, bundle, -1);
    }

    /**
     * 带数据和返回的页面跳转
     * @param  clz
     * @param  bundle
     * */
    public void openActivity(Class<?> clz, Bundle bundle, int requestCode) {
        if(BaseActivityManager.getAppManager().isActivityExist(clz)) return;
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if(bundle != null){
            intent.putExtras(bundle);
        }
        if(requestCode != -1){
            startActivityForResult(intent,requestCode);
        }else{
            startActivity(intent);
        }
    }

    /**
     * 关闭页面
     * @param  clz
     * */
    public void closeActivity(Class<?> clz) {
        BaseActivityManager.getAppManager().finishActivity(clz);
    }

    /**
     * 关闭页面
     * @param  activity
     * */
    public void closeActivity(Activity activity) {
        BaseActivityManager.getAppManager().finishActivity(activity);
    }

    /**
     * 隐藏软键盘
     * */
    public void hideSoftInput(){
        if(getCurrentFocus() != null){
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     * */
    public void showInputMethod(){
        if(getCurrentFocus() != null){
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
        }
    }
}